package com.olivia.sdk.utils;

import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;

// AES加解密工具类
public enum AesUtils {

  // 单例
  X;

  private static final String PASSWORD = "Uxco620as5Xpa5grxG";
  private static final String KEY_ALGORITHM = "AES";
  private static final String SECURE_RANDOM_ALGORITHM = "SHA1PRNG";
  static final SecretKey provideSecretKey = provideSecretKey();
  private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

  private static SecretKey provideSecretKey() {
    try {
      KeyGenerator keyGen = KeyGenerator.getInstance(KEY_ALGORITHM);
      SecureRandom secureRandom = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM);
      secureRandom.setSeed(PASSWORD.getBytes(StandardCharsets.UTF_8));
      keyGen.init(128, secureRandom);
      return new SecretKeySpec(keyGen.generateKey().getEncoded(), KEY_ALGORITHM);
    } catch (Exception e) {
      throw new IllegalArgumentException(e);
    }
  }

  public String encrypt(String content) {
    try {
      Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
      cipher.init(Cipher.ENCRYPT_MODE, provideSecretKey);
      return Hex.encodeHexString(cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)));
    } catch (Exception e) {
      throw new IllegalArgumentException(e);
    }
  }

  public String decrypt(String content) {
    try {
      Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
      cipher.init(Cipher.DECRYPT_MODE, provideSecretKey);
      return new String(cipher.doFinal(Hex.decodeHex(content)));
    } catch (Exception e) {
      throw new IllegalArgumentException(e);
    }
  }
}

